package cn.bjit.service.impl;

import cn.bjit.controller.request.LinkGroupAddRequest;
import cn.bjit.controller.request.LinkGroupUpdateRequest;
import cn.bjit.interceptor.LoginInterceptor;
import cn.bjit.manager.LinkGroupManager;
import cn.bjit.model.LinkGroupDO;
import cn.bjit.service.LinkGroupService;
import cn.bjit.vo.LinkGroupVO;
import org.apache.kafka.common.security.auth.Login;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class LinkGroupServiceImpl implements LinkGroupService {
    @Autowired
    private LinkGroupManager linkGroupManager;

    @Override
    public int add(LinkGroupAddRequest linkGroupAddRequestRequest) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setTitle(linkGroupAddRequestRequest.getTitle());
        linkGroupDO.setAccountNo(accountNo);
        int row = linkGroupManager.add(linkGroupDO);
        return row;
    }

    @Override
    public int del(Long groupId) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        return linkGroupManager.del(groupId, accountNo);
    }


    @Override
    public LinkGroupVO detail(Long groupId) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = linkGroupManager.detail(groupId, accountNo);
        LinkGroupVO linkGroupVO = new LinkGroupVO();
        BeanUtils.copyProperties(linkGroupDO, linkGroupVO);
        return linkGroupVO;
    }

    @Override
    public List<LinkGroupVO> listAllGroup() {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        List<LinkGroupDO> linkGroupDOList =linkGroupManager.listAllGroup(accountNo);
        List<LinkGroupVO> groupVOList=linkGroupDOList.stream().map(obj ->{
            LinkGroupVO linkGroupVO=new LinkGroupVO();
            BeanUtils.copyProperties(obj,linkGroupVO);
            return linkGroupVO;
        }).collect(Collectors.toList());
        return groupVOList;
    }

    @Override
    public int updateById(LinkGroupUpdateRequest linkGroupUpdateRequest) {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO=new LinkGroupDO();
        linkGroupDO.setTitle(linkGroupUpdateRequest.getTitle());
        linkGroupDO.setId(linkGroupUpdateRequest.getId());
        linkGroupDO.setAccountNo(accountNo);
        int row=  linkGroupManager.updateById(linkGroupDO);
        return row;
    }
}
